package com.library.admin.controller;

import com.library.admin.modules.menu.vo.MenuVO;
import com.library.admin.modules.menu.bo.MenuPage;
import com.library.admin.modules.menu.bo.MenuInsert;
import com.library.admin.modules.menu.bo.MenuUpdate;
import com.library.admin.modules.menu.service.MenuService;
import com.library.common.annotation.CurrentUser;
import com.library.common.service.bo.CurrentLoginUser;
import com.library.logging.annotation.LogSys;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.library.common.response.Result;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;

import java.util.List;
import java.util.Objects;


/**
 * @Author xyh
 * @Desc 后台管理菜单表(Menu)表控制层
 * @Date 2023-10-02 15:09:35
 */

@RestController
@RequestMapping("menu")
public class MenuController {

    @Resource
    private MenuService menuService;


    /**
     * 分页查询列表
     *
     * @return 数据
     */
    @LogSys(value = "菜单分页查询列表")
    @GetMapping("/list")
    public Result<IPage<MenuVO>> queryByPage(@Valid MenuPage page) {
        return Result.success(menuService.queryByPage(page));
    }

    /**
     * 根据ID获取数据
     *
     * @return 单个后台管理菜单表数据
     */
    @LogSys(value = "根据菜单ID获取数据")
    @PostMapping("/queryById/{id}")
    public Result<MenuVO> queryById(@PathVariable("id") Integer id) {
        return Result.success(menuService.queryById(id));
    }


    /**
     * 添加后台管理菜单表
     *
     * @return 新增后台管理菜单表数据
     */
    @LogSys(value = "新增后台管理菜单表数据")
    @PostMapping("/insert")
    public Result insert(@Valid @RequestBody MenuInsert param) {
        menuService.insert(param);
        return Result.success();
    }


    /**
     * 编辑后台管理菜单表
     *
     * @return 编辑后台管理菜单表数据
     */
    @LogSys(value = "编辑后台管理菜单表数据")
    @PutMapping("/update")
    public Result update(@Valid @RequestBody MenuUpdate param) {
        if (Objects.isNull(param.getId())) {
            return Result.error("ID不能为空");
        }
        menuService.update(param);
        return Result.success();
    }

    /**
     * 删除后台管理菜单表
     *
     * @return 删除后台管理菜单表数据
     */
    @LogSys(value = "删除后台管理菜单表数据")
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable("id") Integer id) {
        menuService.deleteById(id);
        return Result.success();
    }

    /**
     * 获取全部的菜单数据
     *
     * @return
     */
    @GetMapping("/getAllMenu")
    public Result<List<MenuVO>> getAllMenu() {
        List<MenuVO> menuList = menuService.getTreeMenu(null);
        return Result.success(menuList);
    }

    /**
     * 根据当前用户生成菜单树-左侧菜单栏
     *
     * @param currentLoginUser
     * @return
     */
    @GetMapping("/getMenuList")
    public Result<List<MenuVO>> getMenuList(@CurrentUser CurrentLoginUser currentLoginUser) {
        List<MenuVO> menuList = menuService.getTreeMenu(currentLoginUser.getRoleIds());
        return Result.success(menuList);
    }

}
